/*
  Copyright (c) 2009, Daniel Lerch Hostalot <dlerch@gmail.com>
  All rights reserved.

  Redistribution  and  use  in  source  and  binary  forms,  with  or without 
  modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the  above copyright notice, 
      this list of conditions and the following disclaimer.
    * Redistributions  in  binary  form must reproduce the above copyright 
      notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of the Coelacanth nor the names of its contributors
      may be used to endorse or promote products derived from this software 
      without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
  POSSIBILITY OF SUCH DAMAGE.
*/


#ifndef __COELACANTH_STRING_H__
#define __COELACANTH_STRING_H__

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>



/*
 * @func: string(s)
 * @desc: Create a string variable
 *
 * @param varname: variable name
 * @return: no return value
 */
#define string(varname) char *varname = NULL;

/*
 * @func: string_ptr(s)
 * @desc: Create a string pointer
 *
 * @param varname: variable name
 * @return: no return value
 */
#define string_ptr string


/*
 * @func: string_free(s)
 * @desc: Free memory allocated by string_*() functions
 *
 * @param char *varname: memory allocated string
 * @return: no return value
 */
#define string_free(varname) \
   { \
      if(varname) \
         free(varname); \
   }

/*
 * @func: string_assign_str(s, v)
 * @desc: Assign c-string value to s
 *
 * @param char *varname: memory allocated string or null pointer
 * @param const char *str: value to assign
 * @return: no return value
 */
#define string_assign_str(varname, str) \
   { \
      if(varname) free(varname); \
      varname = malloc(strlen(str)+1); \
      strncpy(varname, str, (strlen(str)+1)); \
      varname[strlen(str)]=0; \
   }

/*
 * @func: string_assign_int(s, v)
 * @desc: Assign int value to s
 * 
 * @param char *s: memory allocated string or null pointer
 * @param int v: value
 * @return char*: pointer to s
 */
#define string_assign_int(varname, intvalue) \
   { \
      if(varname) free(varname); \
      int i=0; \
      int value = intvalue; \
      while(value>0) { i++; value /= 10; } \
      varname = malloc(i+2); \
      snprintf(varname, i+2, "%d", intvalue); \
      varname[i+1]=0; \
   }


/*
 * @func: string_assign_float(s, v)
 * @desc: Assign float value to s
 *
 * @param char *s: memory allocated string or null pointer
 * @param float v: value
 * @return char *: pointer to s
 */
#define string_assign_float(varname, floatvalue) \
   { \
      if(varname) free(varname); \
      int i=0; \
      int value = floatvalue; \
      while(value>0) { i++; value /= 10; } \
      varname = malloc(i+9); \
      snprintf(varname, i+9, "%6f", floatvalue); \
      varname[i+8]=0; \
   }

/*
 * @func: string_to_int(varname)
 * @desc: Cast string to int
 * 
 * @param varname: string
 * @return int: int value
 */
#define string_to_int(varname) (varname==NULL?0:atoi(varname))

/*
 * @func: string_to_float(s)
 * @desc: Cast string to float 
 * 
 * @param char *s: string
 * @return float: float value
 */
float string_to_float(char *s)
{
   if(s==NULL)
      return 0;

   float f;
   sscanf(s, "%f", &f);
   return f;
}

/*
 * @func: string_append(varname, str)
 * @desc: Append c-string value to varname
 * 
 * @param char *varname: memory allocated string or null pointer
 * @param str: value
 * @return: no return value
 */
#define string_append(varname, str) \
   { \
      size_t len = strlen(varname)+strlen(str)+1; \
      varname = realloc(varname, len); \
      if(varname) \
      { \
         strncat(varname, str, len); \
         varname[len-1]=0; \
      } \
   }

/*
 * @func: string_replace(s, pattern, replacement)
 * @desc: Replace pattern by replacement.
 * 
 * @param char *varname: memory allocated string 
 * @param const char *pattern: pattern
 * @param const char *replacement: replacement
 * @return: no return value
 */
#define string_replace(varname, pattern, replacement) \
   { \
      int n_src=0; \
      char *__ptr = varname; \
      char *__ptr2 = varname; \
      while((__ptr=strstr(__ptr, pattern))) { n_src++; __ptr++; } \
      int new_size = strlen(varname) + \
         n_src*(strlen(replacement)-strlen(pattern)); \
      char __string[new_size+1]; \
      __string[0]=0; \
      __ptr = varname; \
      while((__ptr=strstr(__ptr, pattern))) \
      { \
         int len = __ptr-__ptr2; \
         strncat(__string, __ptr2, len); \
         strncat(__string, replacement, sizeof(__string)); \
         __string[sizeof(__string)-1]=0; \
         __ptr2 = __ptr+strlen(pattern); \
         __ptr  = __ptr+strlen(pattern); \
      } \
      int len = __ptr-__ptr2; \
      strncat(__string, __ptr2, len); \
      varname = realloc(varname, new_size+1); \
      strncpy(varname, __string, new_size); \
      varname[new_size]=0; \
   }

/*
 * @func: string_trim(varname)
 * @desc: Remove whitespace from the start and end of the string
 *
 * @param char *varname: memory allocated string 
 * @return: no return value
 */
#define string_trim(varname) \
   { \
      if(varname!=NULL) \
      { \
         char *ptr1 = varname; \
         char *ptr2 = varname; \
         while(isspace(*ptr2)) ptr2++; \
         \
         while(*ptr2!=0) \
            *ptr1++ = *ptr2++; \
         \
         *ptr1--; \
         while(isspace(*ptr1)) ptr1--; \
         \
         *(ptr1+1) = 0; \
      } \
   }

/*
 * @func: string_lpad(varname, padd_char, total_len)
 * @desc: Pads the left side of a string with a specific character
 *
 * @param char *varname: memory allocated string 
 * @param char padd_char: padding character
 * @param size_t total_len: new string length
 * @return: no return value
 */
#define string_lpad(varname, padd_char, total_len) \
   if(strlen(varname)<total_len) \
   { \
      int i; \
      int tlen = total_len; \
      char __string[tlen+1]; \
      strncpy(__string, varname, sizeof(__string)); \
      __string[sizeof(__string)-1]=0; \
      varname = realloc(varname, tlen+1); \
      for(i=0; i<tlen-strlen(__string); i++) \
         varname[i]=padd_char; \
      varname[i]=0; \
      strncat(varname, __string, tlen); \
      varname[total_len]=0; \
   }



/*
 * @func: string_rpad(varname, padd_char, total_len)
 * @desc: Pads the right side of a string with a specific character
 *
 * @param char *varname: memory allocated string 
 * @param char padd_char: padding character
 * @param size_t total_len: new string length
 * @return: no return value
 */
#define string_rpad(varname, padd_char, total_len) \
   if(strlen(varname)<total_len) \
   { \
      int i; \
      int len = strlen(varname); \
      int tlen = total_len; \
      varname = realloc(varname, tlen+1); \
      for(i=len; i<tlen; i++) \
         varname[i]=padd_char; \
      varname[tlen]=0; \
   }

/*
 * @func: string_cut(s, slen, begin, end)
 * @desc: Get a substring
 * 
 * @param char *s: memory allocated string 
 * @param size_t slen: string buffer size
 * @param size_t begin: begin index 
 * @param size_t end: end index 
 * @return: pointer to s
 */
#define string_cut(varname, begin, end) \
   if(varname) \
   { \
      int e = end; \
      if(e>strlen(varname)) \
         e = strlen(varname); \
      \
      if(begin<=e) \
      { \
         char *ptr = varname+begin; \
         strncpy(varname, ptr, e-begin); \
         varname[e-begin]=0; \
      } \
   }

/*
 * @func: string_find(s, pattern)
 * @desc: Find next occurrence of pattern
 *
 * @param char *s: memory allocated string 
 * @param const char *pattern: pattern
 * @return size_t: pattern position, -1 if not found
 */
size_t string_find(char *s, const char *pattern)
{
   char *ptr = strstr(s, pattern);
   if(!ptr)
      return -1;

   return ptr-s;
}

/*
 * @func: string_split(s, token)
 * @desc: Split strings using token as a separator
 *
 * @param char *s: memory allocated string 
 * @param const char *token: separator 
 * @return: pointer s
 */



#endif





